package com.daxiasoftware.xxx.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.daxiasoftware.xxx.entity.Menu;
import com.daxiasoftware.xxx.service.MenuService;

/**
 * 代码生成器，参考源码测试用例：
 * <p>
 * /mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java
 */
@Controller
public class LoginController {
	private static Logger logger = LoggerFactory.getLogger(LoginController.class);
	@Autowired
	private MenuService menuService;
	
	@RequestMapping("admin/login")
	public String login() throws Exception {
		logger.debug("login...");
		return "admin/login";
	}
	
	@RequestMapping("/admin/index")
	public String index(Map<String, Object> map) throws Exception {
		logger.error("index...");
		System.out.println("before...");
		List<Menu> menus = menuService.selectAll();
		List<Menu> level1List = new ArrayList<>();
		for (Menu level1 : menus) {
			if (level1.getLevel() != 1) {
				continue;
			}
			level1List.add(level1);
		}
		
		for (Menu level1 : level1List) {
			for (Menu level2 : menus) {
				if (level2.getLevel() != 2) {
					continue;
				}
				if (level2.getParentId() == level1.getId().longValue()) {
					level1.getChildren().add(level2);
				}
			}
		}
		map.put("menus", level1List);
		System.out.println(level1List);
		return "admin/index";
	}
	
}
